<?php


namespace app\websocket\model;


use think\Db;
use think\Model;

class Customer extends Model
{
    public static function customerConsumption($user){
        $Customer=new self();
        $result=ActionRole::checkUserActionRole($user,'customer');
        if($result===false){
            return false;
        }
        ['search_user'=>$search_user,'group'=>$group]=$result;
        if(!empty($search_user)){
            $where['c.create_user']=['=',$search_user];
        }
        if(!empty($group)){
            $where['c.create_group']=['=',$group];
        }
        $where['c.del_flag']=['=',0];
        $where['o.pay_type']=['=',4];
        $where['c.type']=['=',0];
        $customer_data=$Customer->alias('c')
            ->join('order o','c.id=o.customer_id','left')
            ->where($where)
            ->field('c.id,c.nick_name,round(sum(o.amount_paid),2) paid')
            ->group('c.id having paid >=2000')
            ->order('paid desc')
            ->select()
            ->toArray();
        return $customer_data;
    }

    public static function customerBirthday($user){
        $Customer=new self();
        $result=ActionRole::checkUserActionRole($user,'customer');
        if($result===false){
            return false;
        }
        ['search_user'=>$search_user,'group'=>$group]=$result;
        if(!empty($search_user)){
            $where['create_user']=['=',$search_user];
        }
        if(!empty($group)){
            $where['create_group']=['=',$group];
        }
        $where['del_flag']=['=',0];

        $start_month=date('m');
        $end_month=date('m',strtotime('+10 day'));

        $start_day=date('d');
        $end_day=date('d',strtotime('+10 day'));

        $Customer
            ->where($where);
        if($start_day>$end_day){
            $month_days=date('t');
            $Customer->where(function ($query)use($start_day,$month_days,$start_month,$end_day,$end_month){
                $query->where(function ($query)use($start_day,$month_days,$start_month){
                    $query->where('MONTH(birthday) = '.$start_month)->whereRaw('DAY(birthday) between '.$start_day.' and '.$month_days,[],'and');
                })->whereOr(function ($query)use($end_day,$end_month){
                    $query->where('MONTH(birthday) = '.$end_month)->whereRaw('DAY(birthday) between 01 and '.$end_day,[],'and');
                });
            });
        }else{
            $Customer->whereRaw('MONTH(birthday) between '.$start_month.' and '.$end_month)
                ->whereRaw('DAY(birthday) between '.$start_day.' and '.$end_day);
        }
            $customer_data=$Customer->field('id,customer_name,DATE_FORMAT(birthday,\'%Y-%m-%d\') date_time')
            ->orderRaw('MONTH(birthday),DAY(birthday)')
            ->select()->toArray();
        return $customer_data;
    }
}